from rest_framework.views import APIView
from home.models import AxfFoodtype,AxfGoods
from market.market_serializers import GoodTypeSerializer,GoodsSerializer
from rest_framework.response import Response
from django.conf import settings
from rest_framework import generics
from market.GoodsFilters import GoodsFilter

# Create your views here.


class GoodTypeListView(APIView):
    '''
    get:
    闪送超市获取商品大类型
    '''
    def get(self,request):
        queryset=AxfFoodtype.objects.all()
        goodtype_serializer=GoodTypeSerializer(instance=queryset,many=True)
        return Response({
            'code':200,
            'msg':'请求成功',
            'data':goodtype_serializer.data
        })

"""
# 实际用继承自generics.ListAPIView的写法，不用继承自APIView的写法
class GoodsListView(APIView):
    '''
    get:
    闪送超市展示的商品的列表
    '''
    def get(self,request,*args,**kwargs):
        # params=request.query_params # 获取GET请求传入的参数，返回查询结果集
        tid=request.query_params.get('typeid') # 获取GET请求传入的某个参数的值，注意返回的是字符串类型。typeid是闪送超市左面大类的id
        goods_list=AxfGoods.objects.filter(categoryid=int(tid)) # 商品列表，当前大类的所有商品

        # 过滤子类商品
        childcid=request.query_params.get('childcid',0) # 获取不到时赋默认值0。childcid是闪送超市左面大类下的子类的id
        if int(childcid)>0: # 说明选了大类中的某个具体分类。默认不分类(即选中全部分类)时为0
            goods_list=goods_list.filter(childcid=childcid)

        # 对结果排序并序列化，序列化后才能返回，否则报错
        rule_type=int(request.query_params.get('order_rule',0)) # settings.py中ORDER_RULE_LIST中设置的默认值0，所以获取不到时赋默认值0
        # rule_type默认不排序，所以不用对0的情况判断
        if rule_type==1: # 价格升序
            goods_list=goods_list.order_by('price')
        elif rule_type==2: # 价格降序
            goods_list=goods_list.order_by('-price')
        elif rule_type==3: # 销量升序
            goods_list=goods_list.order_by('productnum')
        elif rule_type==4: # 销量降序
            goods_list=goods_list.order_by('-productnum')
        goods_serializer=GoodsSerializer(instance=goods_list,many=True)
        goods_list=goods_serializer.data

        goodtype=AxfFoodtype.objects.filter(typeid=int(tid)).first()
        childtypenames=goodtype.childtypenames # 大类下的子类名，是字符串
        childtypenames=childtypenames.split('#')
        childtypenames=[i.split(':') for i in childtypenames]
        childtypenames=[{'child_name':item[0],'child_value':item[1]} for item in childtypenames] # 商品分类的子类列表

        return Response({
            'code':200,
            'msg':'请求成功',
            'data':{
                'goods_list':goods_list,
                'order_rule_list':settings.ORDER_RULE_LIST, # 这里order_rule_list的值不能写死，应该事先定义好，否则以后不方便修改
                'foodtype_childname_list':childtypenames
            }
        })
"""


class GoodsListView(generics.ListAPIView):
    '''
    get:
    闪送超市展示的商品的列表
    '''
    queryset = AxfGoods.objects.all()
    serializer_class = GoodsSerializer
    filter_class = GoodsFilter # 指定过滤器类。不能和filter_fields同时使用

    def list(self, request, *args, **kwargs): # 复制的ListAPIView类的get()方法的返回值中的list()方法，删除了分页，修改了返回值，重写这个方法
        queryset = self.filter_queryset(self.get_queryset()) # filter_queryset()方法使用filter_class属性定义的过滤器对结果过滤。filter_queryset()方法中filter_backends是settings.py中REST_FRAMEWORK中DjangoFilterBackend
        serializer = self.get_serializer(queryset, many=True) # 调用serializer_class定义的序列化器
        # 下面复制的继承自APIView类的GoodsListView类
        tid=request.query_params.get('typeid')
        goodtype=AxfFoodtype.objects.filter(typeid=int(tid)).first()
        childtypenames=goodtype.childtypenames
        childtypenames=childtypenames.split('#')
        childtypenames=[i.split(':') for i in childtypenames]
        childtypenames=[{'child_name':item[0],'child_value':item[1]} for item in childtypenames]
        return Response({
            'code':200,
            'msg':'请求成功',
            'data':{
                'goods_list':serializer.data,
                'order_rule_list':settings.ORDER_RULE_LIST,
                'foodtype_childname_list':childtypenames
            }
        })
